/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ServerConfig.java
 *
 * 
 */
package gui;

import entity.Server;
import acc.DataInteraction;
import acc.Validation;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.swing.text.JTextComponent;

/**
 *
 * @author Haitm
 */
public class ServerConfig extends javax.swing.JPanel {

    /** Creates new form ServerConfig */
    public ServerConfig() {
        initComponents();
        this.setSize(310, 250);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtPort = new javax.swing.JTextField();
        txtServername = new javax.swing.JTextField();
        txtDatabasename = new javax.swing.JTextField();
        txtUsername = new javax.swing.JTextField();
        lblPassword = new javax.swing.JLabel();
        lblPort = new javax.swing.JLabel();
        lblServerName = new javax.swing.JLabel();
        lblUserName = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        btnDefault = new javax.swing.JButton();
        lblDatabase = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Server Config", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12), new java.awt.Color(3, 97, 145))); // NOI18N
        setMinimumSize(new java.awt.Dimension(310, 200));
        setPreferredSize(new java.awt.Dimension(330, 200));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtPort.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtPort.setName("txtPort"); // NOI18N
        add(txtPort, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 60, 70, -1));

        txtServername.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtServername.setName("txtServername"); // NOI18N
        add(txtServername, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 60, 100, -1));

        txtDatabasename.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtDatabasename.setName("txtDatabasename"); // NOI18N
        add(txtDatabasename, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 30, 210, -1));

        txtUsername.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtUsername.setName("txtUsername"); // NOI18N
        add(txtUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 90, 210, -1));

        lblPassword.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblPassword.setText("Password");
        lblPassword.setName("lblPassword"); // NOI18N
        add(lblPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, -1, -1));

        lblPort.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblPort.setText("Port");
        lblPort.setName("lblPort"); // NOI18N
        add(lblPort, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 60, -1, -1));

        lblServerName.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblServerName.setText("Server Name");
        lblServerName.setName("lblServerName"); // NOI18N
        add(lblServerName, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, -1, -1));

        lblUserName.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblUserName.setText("User Name");
        lblUserName.setName("lblUserName"); // NOI18N
        add(lblUserName, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, -1, -1));

        btnSave.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnSave.setForeground(new java.awt.Color(3, 97, 145));
        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/btn_acceptmini.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.setMargin(new java.awt.Insets(0, 2, 0, 2));
        btnSave.setMaximumSize(new java.awt.Dimension(55, 25));
        btnSave.setMinimumSize(new java.awt.Dimension(55, 25));
        btnSave.setName("btnSave"); // NOI18N
        btnSave.setPreferredSize(new java.awt.Dimension(55, 25));
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        add(btnSave, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 150, 60, 25));

        btnDefault.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnDefault.setForeground(new java.awt.Color(3, 97, 145));
        btnDefault.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/refreshmini.png"))); // NOI18N
        btnDefault.setText("Default");
        btnDefault.setMargin(new java.awt.Insets(0, 2, 0, 2));
        btnDefault.setMaximumSize(new java.awt.Dimension(67, 25));
        btnDefault.setMinimumSize(new java.awt.Dimension(67, 25));
        btnDefault.setName("btnDefault"); // NOI18N
        btnDefault.setPreferredSize(new java.awt.Dimension(67, 25));
        btnDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDefaultActionPerformed(evt);
            }
        });
        add(btnDefault, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 150, 80, 25));

        lblDatabase.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblDatabase.setText("Database");
        lblDatabase.setName("lblDatabase"); // NOI18N
        add(lblDatabase, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, -1, -1));

        txtPassword.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtPassword.setName("txtPassword"); // NOI18N
        add(txtPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 120, 210, 20));
    }// </editor-fold>//GEN-END:initComponents

    private void btnDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDefaultActionPerformed
        txtDatabasename.setText("CampaignInformationSystem");
        txtServername.setText("localhost");
        txtPort.setText("1433");
        txtUsername.setText("sa");
        txtPassword.setText("123456");
        saveToFile();
        DataInteraction.configServer();
    }//GEN-LAST:event_btnDefaultActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        saveToFile();
        DataInteraction.configServer();
        System.out.println("oki config server!!");
    }//GEN-LAST:event_btnSaveActionPerformed

    private void saveToFile() {
        if (!Validation.validateComponents(new JTextComponent[]{txtDatabasename, txtServername, txtPort, txtUsername, txtPassword})) {
            return;
        }
        if (!Validation.isInt(txtPort.getText())) {
            Validation.showMessage("Port must be a number");
            return;
        }

        File file = new File(Server.configFile);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        try {
            FileOutputStream fo = new FileOutputStream(file);
            ObjectOutputStream os = new ObjectOutputStream(fo);
            os.writeObject(getServer());
            os.flush();
            os.close();
            //new ShowMessageDialog(this, true,"Config Suscessfuly !",ShowMessageDialog.MESSAGE);

        } catch (Exception e) {
            e.printStackTrace();
            //new ShowMessageDialog(null, true,"Config False !",ShowMessageDialog.MESSAGE);
        }
    }

    private Server getServer() {
        Server sv = new Server();
        sv.setDatabaseName(txtDatabasename.getText());
        sv.setServerName(txtServername.getText());
        sv.setPort(txtPort.getText());
        sv.setUserName(txtUsername.getText());
        sv.setPassword(txtPassword.getText());

        return sv;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDefault;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel lblDatabase;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblPort;
    private javax.swing.JLabel lblServerName;
    private javax.swing.JLabel lblUserName;
    private javax.swing.JTextField txtDatabasename;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextField txtServername;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
